package com.example.demo.controller;

import com.example.demo.component.OnlineUserManger;
import com.example.demo.model.Friend;
import com.example.demo.model.FriendRequest;
import com.example.demo.model.User;
import com.example.demo.myBatisMapper.FriendMapper;
import com.example.demo.myBatisMapper.FriendRequestMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class AddFriendController {

    @Autowired
    private FriendMapper friendMapper;

    @Autowired
    private FriendRequestMapper friendRequestMapper;

    @Autowired
    private OnlineUserManger onlineUserManger;

    @Autowired
    private ObjectMapper objectMapper;

    //用来查询要添加的好友信息
    @RequestMapping("/searchFriend")
    public List<Friend> getSearchFriend(String searchInput, HttpServletRequest request){
        System.out.println(searchInput);
        if (request.getSession(false) == null){
            System.out.println("getSearchFriend 用户尚未登录!");
            return new ArrayList<Friend>();
        }
        if (searchInput == null){
            System.out.println("searchInput 为null");
            return new ArrayList<Friend>();
        }
        List<Friend> friends = friendMapper.selectUserByInput(searchInput);
        System.out.println(friends.size());
        return friends;
    }

    @Transactional
    @RequestMapping("/friendRequest")
    public int getFriendRequest(int toUserId, String reasonValue,String selfUsername, @SessionAttribute("user") User user) throws IOException {
        int resp = 0;
        if (toUserId < 1){
            System.out.println("getFriendRequest userId有误!");
            return resp;
        }
        //判断当前要添加的好友是否已经存在
        List<Integer> friends = friendRequestMapper.selectForFriend(user.getUserId(),toUserId);
        if (friends!=null) {
            for (Integer friendId : friends) {
                if (friendId == toUserId) {
                    resp = 2;
                    return resp;
                }
            }
        }
        //判断当前用户是否已经向指定用户发送过好友请求了
        FriendRequest friendRequest2 = friendRequestMapper.selectFriendRequest(user.getUserId(),toUserId);
        if (friendRequest2 != null){
            resp = 3;
            return resp;
        }
        FriendRequest friendRequest = new FriendRequest();
        friendRequest.setFromId(user.getUserId());
        friendRequest.setToUserId(toUserId);
        friendRequest.setReasonValue(reasonValue);
        resp = friendRequestMapper.addFriendRequest(friendRequest);
        //如果用户在线,要实时的向在线用户发送好友请求
        //判断该用户是否在线
        WebSocketSession webSocketSession = onlineUserManger.getSession(toUserId);
        if (webSocketSession!=null){
            //用户在线,实时发送消息请求
            FriendRequestResponse friendRequestResponse = new FriendRequestResponse();
            friendRequestResponse.setFromId(user.getUserId());
            friendRequestResponse.setToUserId(toUserId);
            friendRequestResponse.setContent(reasonValue);
            friendRequestResponse.setFromName(selfUsername);
            String respJson = objectMapper.writeValueAsString(friendRequestResponse);
            webSocketSession.sendMessage(new TextMessage(respJson));
            System.out.println("好友请求已经发送:"+respJson);
        }
        return resp;
    }

    @RequestMapping("/accept")
    @Transactional
    int acceptRequest(int fromId,int toUserId){
        int resp = 0;
        //接收好友请求
        try {
            //1.在好友表里插入记录 1->2
            friendMapper.addFriend(fromId,toUserId);
            //2.在好友表里插入记录 2->1
            friendMapper.addFriend(toUserId,fromId);
            //3.删除好友请求表里的请求
            friendRequestMapper.deleteRequest(fromId,toUserId);
        } catch (Exception e) {
            System.out.println("acceptRequest执行失败!");
            return resp;
        }
        resp = 1;
        return resp;
    }

    @RequestMapping("/refuse")
    int refuseRequest(int fromId,int toUserId){
        int resp = 0;
        try {
            //删除好友请求表里的请求
            friendRequestMapper.deleteRequest(fromId,toUserId);
        } catch (Exception e) {
            System.out.println("refuseRequest执行失败!");
            return resp;
        }
        resp = 1;
        return resp;
    }

    @RequestMapping("/getHistoryRequest")
    public List<FriendRequestResponse> getHistoryRequest(@SessionAttribute("user") User user){
        //获取指定用户的好友历史请求
        if (user == null){
            //在当前会话中获取不到用户
            System.out.println("getHistoryRequest user == null");
            return null;
        }
        //在friend_request表中查询历史请求
        List<FriendRequestResponse> list= friendRequestMapper.getHistoryRequest(user.getUserId());
        for (FriendRequestResponse friendRequestResponse:list) {
            System.out.println(friendRequestResponse.getFromName()+friendRequestResponse.getContent());
        }
        return list;
    }
}
